package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.User;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.IUpdate;
import com.by.query.UserQuery;
import com.by.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.DispatcherServlet;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author wang
 * @version 版本号 ${}
 * @className UserController 用户控制器类，继承自控制器BaseController
 * 该类用于处理与用户相关的HTTP请求
 * @日期 2024/9/4 14:56
 * @since jdk1.8
 */
@RestController
@RequestMapping ("v1")
@Api (tags = "用户管理")
public class UserController extends BaseController {

    @Autowired
    UserService userService;


/*    @Autowired
    HttpServletRequest request;

    @ApiOperation ("新增用户")
    @PostMapping ("/user")
    public ResultBean save(@Validated ({ISave.class}) @RequestBody User user, BindingResult result) {
        //调用BaseController中的方法
        validate(result);

        int rs = userService.save(user);
        return saveResult(rs);
    }


    /**
     * 修改用户
     *
     * @param user 待更新的用户对象
     * @return 操作结果
     *//*
    @ApiOperation ("修改用户")
    @PutMapping ("/user")
    public ResultBean update(@Validated ({IUpdate.class}) @RequestBody User user, BindingResult result) {
        validate(result);
        int rs = userService.update(user);
        return updateResult(rs);
    }


    /**
     * 删除用户
     *
     * @param user_id 用户ID
     * @return 操作结果
     *//*
    @ApiImplicitParam (name = "user_id", value = "用户id", example = "123")//example 属性来指定每个字段的示例值。
    @ApiOperation ("删除")
    @DeleteMapping ("/user")
    public ResultBean delete(@Validated ({IUpdate.class, ISave.class}) @RequestParam int user_id, BindingResult result) {//RequestParam中的defaultValue()有给后边属性赋默认值的功能
        validate(result);
        int de = userService.delete(user_id);
        return deleteResult(de);
    }


    /**
     * 查看用户列表
     *
     * @param map 分页查询参数
     * @return 用户列表
     *//*
    @ApiImplicitParams ({
            @ApiImplicitParam (name = "page", value = "当前第几页", dataTypeClass = Integer.class, required = true, paramType = "query", example = "123"),
            @ApiImplicitParam (name = "pageSize", value = "每页记录数", dataTypeClass = Integer.class, required = true, paramType = "query", example = "123")

    })
    @ApiOperation ("查看用户")
    @GetMapping ("/user")
    public ResultBean<PageBean> query(@Validated ({IUpdate.class, ISave.class}) @ApiIgnore @RequestParam Map map) {
        DispatcherServlet dispatcherServlet;

        String path=request.getServletContext().getRealPath("/");
        System.out.println(path);

        PageBean bean = userService.query(map);
        return success("用户列表", bean);
    }

    /**
     * 根据用户ID查找用户
     *
     * @param user_id 用户ID
     * @return 用户对象
     */
    /*
    @GetMapping ("/users")
    @ApiOperation ("根据用户id查找用户")
    public User get(@RequestParam int user_id) {  //RequestParam有给后边属性赋默认值的功能



        User user = userService.get(user_id);
        return user;
    }*/
    
    
    @ApiImplicitParam(name = "account", paramType = "query", value = "用户名", dataTypeClass = String.class, required = true)
    @ApiOperation("账号唯一检查")
    @GetMapping("/user/check")
    public ResultBean checkUser(@RequestParam(name="account",required = true) String account){
        int rs=userService.queryUserByAccount(account);
        String tmp=rs>0?"no":"yes";
        return success("账号唯一检查",tmp);
    }
    
    @ApiOperation("用户列表")
    @PostMapping("/user/query")
    public ResultBean query(@RequestBody UserQuery query) {
        Map params= BeanUtil.beanToMap(query);
        PageBean bean = userService.query(params);
        return success("用户列表",bean);
    }
    
}
